<?php

/*  
==================================================================
1. KOMPETENSI KELOMPOK
==================================================================
*/

function list_kompetensi_kelompok(){
	my_set_code_js('
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=kompetensi_kelompok_delete&id=\'+id;
			}
			return false;
		}
	');

	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'),  
		'Kelompok Kompetensi'=>array('style'=>'border-bottom:2px solid;width:30%'),  
		'Jenis Kompetensi'=>array('style'=>'border-bottom:2px solid;width:30%'),  
		'Catatan'=>array('style'=>'border-bottom:2px solid;width:25%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);

	$query = "SELECT * FROM kompetensi_kelompok a INNER JOIN kompetensi_jenis b
	ON a.kompetensi_jenis_id=b.kompetensi_jenis_id";
	$result = my_query($query);
	$i = 0;
	$row=array();
	while($ey = my_fetch_array($result)){
		$i++;
		$editproperty = array(
						'href'=>'index.php?com=kompetensi&task=kompetensi_kelompok_create&id='.$ey['kompetensi_kelompok_id'],
						'rel'=>'facebox',
						'title'=>'Edit'
				);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );
		$detailproperty = array(
						'href'=>'index.php?com=kompetensi&task=list_kompetensi_unit&kid='.$ey['kompetensi_kelompok_id'],
						'title'=>'Unit Kompetensi'
				);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );

		$query="SELECT kompetensi_unit_id FROM kompetensi_unit 
			WHERE kompetensi_kelompok_id=".$ey['kompetensi_kelompok_id'] ;
		if (my_num_rows( my_query($query)) >0){
			$delete_button = button_icon( 'b_drop_off.png' , ''  );
		}else{
			$deleteproperty = array(
				'href'=>'javascript:;',
				'title'=>'Delete',
				'onclick'=>'confirmDelete('.$ey['kompetensi_kelompok_id'].')',
			);
			$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		}	
 
		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'nama'=>  $ey['nama_kompetensi_kelompok'] ,   
			'dir'=>  $ey['nama_kompetensi_jenis'] ,   
			'catatan'=>  (trim($ey['note'] ) == '' ?'-' : trim($ey['note'] )  ),   
			'operasi'=> position_text_align(  $edit_button.' '.$detail_button.' '.$delete_button, 'right'),  
		);
		 
	}
	$datas = table_rows($row);   
	return table_builder($header , $datas , 5 , false );
} 

function form_kompetensi_kelompok_create($id = 0){
	$view = form_header( "kompetensi_kelompok" , "kompetensi_kelompok"  );
 	$fields = my_get_data_by_id('kompetensi_kelompok','kompetensi_kelompok_id',$id);
	 
	$nama = array(
				'name'=>'nama_kompetensi_kelompok',
				'value'=>( isset($_POST['nama_kompetensi_kelompok']) ? $_POST['nama_kompetensi_kelompok'] : $fields['nama_kompetensi_kelompok'] ),
				'id'=>'nama_kompetensi_kelompok',
				'type'=>'text',
				'size'=>'45'
			);
	$form_field_nama = form_dynamic($nama);
	$view .= form_field_display( $form_field_nama   , "Kelompok kompetensi");
	
	$dirs = array();
	$query = "SELECT * FROM kompetensi_jenis";
	$res = my_query($query);
	while($row = my_fetch_array($res)){
		$dirs[$row['kompetensi_jenis_id']] = $row['nama_kompetensi_jenis'];
	}
	$dir = array(
				'name'=>'kompetensi_jenis_id',
				'value'=>( isset($_POST['kompetensi_jenis_id']) ? $_POST['kompetensi_jenis_id'] : $fields['kompetensi_jenis_id'] ),
				'id'=>'kompetensi_jenis_id', 
			);
	$form_field_dir = form_dropdown($dir , $dirs);
	$view .= form_field_display( $form_field_dir   , "Jenis kompetensi");
	
	$c = array(
				'name'=>'note',
				'value'=>( isset($_POST['note']) ? $_POST['note'] : $fields['note'] ),
				'id'=>'note',  'rows'=>'2'
			);
	$form_field_nama = form_textarea($c);
	$view .= form_field_display( $form_field_nama   , "Catatan");
	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
} 
	 
function form_kompetensi_kelompok_submit($id=0){
	$datas = array(
		'nama_kompetensi_kelompok'=>my_type_data_str( $_POST['nama_kompetensi_kelompok'] ),
		'kompetensi_jenis_id'=>my_type_data_str( $_POST['kompetensi_jenis_id'] ),
		'note'=>my_type_data_str($_POST['note']),
	);
	if($id > 0){
		return my_update_record('kompetensi_kelompok' ,'kompetensi_kelompok_id' , $id , $datas);
	}
	
	return my_insert_record('kompetensi_kelompok' , $datas);
} 

function form_kompetensi_kelompok_validate($kompetensi_kelompok_id){
	$errsubmit = false;
	$err = array();
	 
	$nama =  trim($_POST["nama_kompetensi_kelompok"]); 
	if( $nama == ''){
		$errsubmit = true;
		$err[] = "Kelompok kompetensi belum diisi";
	}

	$query="SELECT kompetensi_kelompok_id FROM kompetensi_kelompok 
		WHERE nama_kompetensi_kelompok='".$nama.
		"' AND kompetensi_kelompok_id<>".$kompetensi_kelompok_id; 
	if (my_num_rows( my_query($query)) >0){
		$errsubmit = true;
		$err[] = "Nama sudah ada";	
	}
    
	$jenis = $_POST["kompetensi_jenis_id"];
	if($jenis == '0'){
		$errsubmit = true;
		$err[] = "Jenis belum diisi";
	}
	 
	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}

function delete_kompetensi_kelompok($kompetensi_kelompok_id){
	$query = "DELETE FROM kompetensi_kelompok WHERE kompetensi_kelompok_id ={$kompetensi_kelompok_id} ";
	return my_query($query);
}	

/*  
==================================================================
2. KOMPETENSI UNIT
==================================================================
*/

function list_kompetensi_unit($kid = 0){
	my_set_code_js('
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=kompetensi_unit_delete&id=\'+id;
			}
			return false;
		}
	');
	$kid = (int) $kid;
	if($kid == 0)my_direct($_SERVER['HTTP_REFERER']);
		$url = 'index.php?com=kompetensi&task=delete_unit_kompetensi&id=';
		remove_data_load_js( $url );
		$header = array(
			'#'=>array('style'=>'border-bottom:2px solid;width:5%'),  
			'Unit kompetensi'=>array('style'=>'border-bottom:2px solid;width:85%'),  
			' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);
 
	$query = "SELECT * FROM kompetensi_unit WHERE kompetensi_kelompok_id ={$kid}";
 	$result = my_query($query);
	$i = 0;
	$row=array();
	while($ey = my_fetch_array($result)){
		$i++;
		$editproperty = array(
						'href'=>'index.php?com=kompetensi&task=kompetensi_unit_create&kid='.$kid.'&id='.$ey['kompetensi_unit_id'],
						'rel'=>'facebox',
						'title'=>'Kompetensi'
				);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );
		$detailproperty = array(
						'href'=>'index.php?com=kompetensi&task=list_kompetensi&uid='.$ey['kompetensi_unit_id'].'&kid='.$kid,
						'title'=>'Kompetensi'
				);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );

		$query="SELECT kompetensi_id FROM kompetensi 
			WHERE kompetensi_unit_id=".$ey['kompetensi_unit_id'] ;
		if (my_num_rows( my_query($query)) >0){
			$delete_button = button_icon( 'b_drop_off.png' , ''  );
		}else{
			$deleteproperty = array(
				'href'=>'javascript:;',
				'title'=>'Delete',
				'onclick'=>'confirmDelete('.$ey['kompetensi_unit_id'].')',
			);
			$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		}	
		

		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'nama'=>  $ey['nama_kompetensi_unit'] ,    
			'operasi'=> position_text_align(  $edit_button.' '.$detail_button.' '.$delete_button, 'right'),  
		);
		 
	}
	$datas = table_rows($row); 
	$kompetensi_kelompok = my_get_data_by_id('kompetensi_kelompok','kompetensi_kelompok_id',$kid); 	 
	$jenis_kompetensi =my_get_data_by_id('kompetensi_jenis','kompetensi_jenis_id',$kompetensi_kelompok['kompetensi_jenis_id']); 	 
	$view ='<table width="90%" cellspacing="0" cellpadding="4"><tr>'; 
	$view .= form_header( "Unit Kompetensi" , "Unit Kompetensi"  );/*
	$view .= '<tr><td><td><div style="width:100%;text-align:right;">
	<input type="button" value="Tambah Data" onclick="javascript: jQuery.facebox({ ajax: \'index.php?com=kompetensi&task=kompetensi_unit_create&kid='.$kid.'&id=0\' });" class="main_button" /> 
	<input type="button" value="Cetak" class="main_button" onclick="javascript:window.open(\'index.php?com=report&task=list_kompetensi_unit&id='.$id.'\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,resizable=0\');"/> 	
	<input type="button" value="Back" class="main_button" onclick="javascript:location.href=\'index.php?com=kompetensi\'"/></div></td></tr>'   ; 
	*/$view .= form_field_display( '<span class="label_form">'.ucfirst($kompetensi_kelompok['nama_kompetensi_kelompok']).'</span>', "<b>Kelompok kompetensi</b>"    ); 
	$view .= form_field_display( '<span class="label_form">'.$jenis_kompetensi['nama_kompetensi_jenis'].'</span>'   , "<b>Jenis kompetensi</b>"    ); 
	return  $view. table_builder($header , $datas ,  3 , false );
}

function form_kompetensi_unit_create($kid, $id = 0){
	$view = form_header( "kompetensi_unit" , "kompetensi_unit"  );
 	$fields = my_get_data_by_id('kompetensi_unit','kompetensi_unit_id',$id);
 	$kelompok = my_get_data_by_id('kompetensi_kelompok','kompetensi_kelompok_id',$kid); 
	$nama = array(
				'name'=>'nama_kompetensi_unit',
				'value'=>( isset($_POST['nama_kompetensi_unit']) ? $_POST['nama_kompetensi_unit'] : $fields['nama_kompetensi_unit'] ),
				'id'=>'nama_kompetensi_unit',
				'type'=>'text',
				'size'=>'45'
			);
	$form_field_nama = form_dynamic($nama);
	$view .= form_field_display( $form_field_nama   , "Unit kompetensi");
	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
} 
	 
function form_kompetensi_unit_submit($kid , $id =0){
	$datas = array(
		'kompetensi_kelompok_id'=>my_type_data_int($kid),	
		'nama_kompetensi_unit'=>my_type_data_str( $_POST['nama_kompetensi_unit'] ),
	);
	
	if($id > 0){
		return my_update_record('kompetensi_unit' ,'kompetensi_unit_id' , $id , $datas);
	}
	return my_insert_record('kompetensi_unit' , $datas);
} 

function form_kompetensi_unit_validate($kompetensi_unit_id){
	$errsubmit = false;
	$err = array();
	 
	$nama =  trim($_POST["nama_kompetensi_unit"]); 
	if( $nama == ''){
		$errsubmit = true;
		$err[] = "Unit kompetensi belum diisi";
	}

	$query="SELECT kompetensi_unit_id FROM kompetensi_unit 
		WHERE nama_kompetensi_unit='".$nama.
		"' AND kompetensi_unit_id<>".$kompetensi_unit_id; 
	if (my_num_rows( my_query($query)) >0){
		$errsubmit = true;
		$err[] = "Nama sudah ada";	
	}
    
	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}

function delete_kompetensi_unit($kompetensi_unit_id){
	$query = "DELETE FROM kompetensi_unit WHERE kompetensi_unit_id ={$kompetensi_unit_id} ";
	return my_query($query);
}	

/*  
==================================================================
3. KOMPETENSI
==================================================================
*/

function list_kompetensi($uid = 0){
	my_set_code_js('
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=kompetensi_delete&id=\'+id;
			}
			return false;
		}
	');
	$uid = (int) $uid;
	if($uid == 0)my_direct($_SERVER['HTTP_REFERER']);
		$url = 'index.php?com=kompetensi&task=delete_unit_kompetensi&id=';
		remove_data_load_js( $url );
		$header = array(
			'#'=>array('style'=>'border-bottom:2px solid;width:5%'),  
			'Kode'=>array('style'=>'border-bottom:2px solid;width:15%'),  
			'Nama Kompetensi'=>array('style'=>'border-bottom:2px solid;width:70%'),  
			' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);
 
	$query = "SELECT * FROM kompetensi WHERE kompetensi_unit_id ={$uid}";
 	$result = my_query($query);
	$i = 0;
	$row=array();
	while($ey = my_fetch_array($result)){
		$i++;
		$editproperty = array(
						'href'=>'index.php?com=kompetensi&task=kompetensi_create&uid='.$uid.'&id='.$ey['kompetensi_id'],
						'rel'=>'facebox',
						'title'=>'Edit'
				);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );
		$detailproperty = array(
						'href'=>'index.php?com=kompetensi&task=list_kompetensi_indikator&pid='.$ey['kompetensi_id'],
						'title'=>'Detail'
				);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );

		$query="SELECT kompetensi_indikator_id FROM kompetensi_indikator
			WHERE kompetensi_id=".$ey['kompetensi_id'] ;

		if (my_num_rows( my_query($query)) >0){
			$delete_button = button_icon( 'b_drop_off.png' , ''  );
		}else{
			$deleteproperty = array(
				'href'=>'javascript:;',
				'title'=>'Delete',
				'onclick'=>'confirmDelete('.$ey['kompetensi_id'].')',
			);
			$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		}	
		
		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'kode'=>  $ey['kode_kompetensi'] ,    
			'nama'=>  $ey['nama_kompetensi'] ,    
			'operasi'=> position_text_align(  $edit_button.' '.$detail_button.' '.$delete_button, 'right'),  
		);
		 
	}
	
	$datas = table_rows($row); 
	$kompetensi_unit = my_get_data_by_id('kompetensi_unit','kompetensi_unit_id',$uid); 	 
	$kompetensi_kelompok = my_get_data_by_id('kompetensi_kelompok','kompetensi_kelompok_id',$kompetensi_unit['kompetensi_kelompok_id']); 
	$jenis_kompetensi =my_get_data_by_id('kompetensi_jenis','kompetensi_jenis_id',$kompetensi_kelompok['kompetensi_jenis_id']); 	 

	$view ='<table width="90%" cellspacing="0" cellpadding="4"><tr>'; 
	$view .= form_header( "Unit Kompetensi" , "Unit Kompetensi"  );
	/*$view .= '<tr><td><td><div style="width:100%;text-align:right;">
	<input type="button" value="Tambah Data" onclick="javascript: jQuery.facebox({ ajax: \'index.php?com=kompetensi&task=kompetensi_create&uid='.$uid.'&id=0\' });" class="main_button" /> 
	<input type="button" value="Cetak" class="main_button" onclick="javascript:window.open(\'index.php?com=report&task=list_kompetensi&id='.$id.'\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,resizable=0\');"/> 	
	<input type="button" value="Back" class="main_button" onclick="javascript:location.href=\'index.php?com=kompetensi&task=list_kompetensi_unit&kid='.$kompetensi_unit['kompetensi_kelompok_id'].'\'"/></div></td></tr>'   ; 
	*/$view .= form_field_display( '<span class="label_form">'.$kompetensi_kelompok['nama_kompetensi_kelompok'].'</span>', "<b>Kelompok kompetensi</b>"); 
	$view .= form_field_display( '<span class="label_form">'.$jenis_kompetensi['nama_kompetensi_jenis'].'</span>'   , "<b>Jenis kompetensi</b>"    ); 
	$view .= form_field_display( '<span class="label_form">'.ucfirst($kompetensi_unit['nama_kompetensi_unit']).'</span>', "<b>Unit kompetensi</b>"    ); 
	return  $view. table_builder($header , $datas , 4 , false );
}

function form_kompetensi_create($uid, $id = 0){
	$view = form_header( "kompetensi" , "kompetensi"  );
 	$fields = my_get_data_by_id('kompetensi','kompetensi_id',$id);

	$kode = array(
				'name'=>'kode_kompetensi',
				'value'=>( isset($_POST['kode_kompetensi']) ? $_POST['kode_kompetensi'] : $fields['kode_kompetensi'] ),
				'id'=>'kode_kompetensi',
				'type'=>'text',
				'size'=>'20'
			);
	$form_field_kode = form_dynamic($kode);
	$view .= form_field_display( $form_field_kode   , "Kode Kompetensi");	

	$c = array(
				'name'=>'nama_kompetensi',
				'value'=>( isset($_POST['nama_kompetensi']) ? $_POST['nama_kompetensi'] : $fields['nama_kompetensi'] ),
				'id'=>'nama_kompetensi',  
				'rows'=>'3',
				'size'=>'45'
			);
	$form_field_nama = form_textarea($c);
	$view .= form_field_display( $form_field_nama   , "Nama Kompetensi");

	$d = array(
				'name'=>'note',
				'value'=>( isset($_POST['note']) ? $_POST['note'] : $fields['note'] ),
				'id'=>'note',  
				'rows'=>'3',
				'size'=>'45'
			);
	$form_field_note = form_textarea($d);
	$view .= form_field_display( $form_field_note   , "Catatan");	
	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
} 
	 
function form_kompetensi_submit($uid , $id =0){
	$datas = array(
		'kompetensi_unit_id'=>my_type_data_int($uid),	
		'kode_kompetensi'=>my_type_data_str( $_POST['kode_kompetensi'] ),
		'nama_kompetensi'=>my_type_data_str( $_POST['nama_kompetensi'] ),
		'note'=>my_type_data_str( $_POST['note'] ),
	);
	
	if($id > 0){
		return my_update_record('kompetensi' ,'kompetensi_id' , $id , $datas);
	}
	return my_insert_record('kompetensi' , $datas);
} 

function form_kompetensi_validate($kompetensi_id){
	$errsubmit = false;
	$err = array();

	$kode =  trim($_POST["kode_kompetensi"]); 
	if( $kode == ''){
		$errsubmit = true;
		$err[] = "Kode kompetensi belum diisi";
	}
/*
	$query="SELECT kompetensi_id FROM kompetensi 
		WHERE kode_kompetensi='".$kode.
		"' AND kompetensi_id<>".$kompetensi_id; 
	if (my_num_rows( my_query($query)) >0){
		$errsubmit = true;
		$err[] = "Kode sudah ada";	
	}
*/	
	$nama =  trim($_POST["nama_kompetensi"]); 
	if( $nama == ''){
		$errsubmit = true;
		$err[] = "Nama kompetensi belum diisi";
	}
/*
	$query="SELECT kompetensi_id FROM kompetensi 
		WHERE nama_kompetensi='".$nama.
		"' AND kompetensi_id<>".$kompetensi_id; 
	if (my_num_rows( my_query($query)) >0){
		$errsubmit = true;
		$err[] = "Nama sudah ada";	
	}
 */   
	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}

function delete_kompetensi($kompetensi_id){
	$query = "DELETE FROM kompetensi WHERE kompetensi_id ={$kompetensi_id} ";
	return my_query($query);
}	


/*  
==================================================================
4. KOMPETENSI INDIKATOR
==================================================================
*/

function list_kompetensi_indikator($pid = 0){
	my_set_code_js('
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=kompetensi_indikator_delete&id=\'+id;
			}
			return false;
		}
	');
	$pid = (int) $pid;
	if($pid == 0)my_direct($_SERVER['HTTP_REFERER']);
		$url = 'index.php?com=kompetensi&task=delete_kompetensi_indikator&id=';
		remove_data_load_js( $url );
		$header = array(
			'Tingkat'=>array('style'=>'border-bottom:2px solid;width:10%'),  
			'Indikator'=>array('style'=>'border-bottom:2px solid;width:80%'),  
			' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);
 
	$query = "SELECT * FROM kompetensi_indikator WHERE kompetensi_id ={$pid} ORDER BY tingkat_id";
 	$result = my_query($query);
	$row=array();
	while($ey = my_fetch_array($result)){
		$editproperty = array(
						'href'=>'index.php?com=kompetensi&task=kompetensi_indikator_create&pid='.$pid.'&id='.$ey['kompetensi_indikator_id'],
						'rel'=>'facebox',
						'title'=>'Edit'
				);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );

		/*
		$query="SELECT kompetensi_indikator_id FROM kompetensi_indikator
			WHERE kompetensi_id=".$ey['kompetensi_id'] ;

		if (my_num_rows( my_query($query)) >0){
			$delete_button = button_icon( 'b_drop_off.png' , ''  );
		}else{
		
		}
		*/
			$deleteproperty = array(
				'href'=>'javascript:;',
				'title'=>'Delete',
				'onclick'=>'confirmDelete('.$ey['kompetensi_indikator_id'].')',
			);
			$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
			
		$row[] = array(
			'tingkat'=>  $ey['tingkat_id'] ,    
			'indikator'=>  $ey['indikator'] ,    
			'operasi'=> position_text_align(  $edit_button.' '.$delete_button, 'right'),  
		);
		 
	}
	
	$datas = table_rows($row); 
	$kompetensi = my_get_data_by_id('kompetensi','kompetensi_id',$pid); 	 
	$kompetensi_unit = my_get_data_by_id('kompetensi_unit','kompetensi_unit_id',$kompetensi['kompetensi_unit_id']); 	 
	$kompetensi_kelompok = my_get_data_by_id('kompetensi_kelompok','kompetensi_kelompok_id',$kompetensi_unit['kompetensi_kelompok_id']); 
	$jenis_kompetensi =my_get_data_by_id('kompetensi_jenis','kompetensi_jenis_id',$kompetensi_kelompok['kompetensi_jenis_id']); 	 

	$view ='<table width="90%" cellspacing="0" cellpadding="4"><tr>'; 
	$view .= form_header( "Unit Kompetensi" , "Unit Kompetensi"  );
	/*$view .= '<tr><td><td><div style="width:100%;text-align:right;">
	<input type="button" value="Tambah Data" onclick="javascript: jQuery.facebox({ ajax: \'index.php?com=kompetensi&task=kompetensi_indikator_create&pid='.$pid.'&id=0\' });" class="main_button" /> 
	<input type="button" value="Cetak" class="main_button" onclick="javascript:window.open(\'index.php?com=report&task=list_kompetensi_indikator&id='.$id.'\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,resizable=0\');"/> 	
	<input type="button" value="Back" class="main_button" onclick="javascript:location.href=\'index.php?com=kompetensi&task=list_kompetensi&uid='.$kompetensi['kompetensi_unit_id'].'\'"/></div></td></tr>'   ; 
	*/$view .= form_field_display( '<span class="label_form">'.$kompetensi_kelompok['nama_kompetensi_kelompok'].'</span>', "<b>Kelompok kompetensi</b>"); 
	$view .= form_field_display( '<span class="label_form">'.$jenis_kompetensi['nama_kompetensi_jenis'].'</span>'   , "<b>Jenis kompetensi</b>"    ); 
	$view .= form_field_display( '<span class="label_form">'.$kompetensi_unit['nama_kompetensi_unit'].'</span>', "<b>Unit kompetensi</b>"    ); 
	$view .= form_field_display( '<span class="label_form">'.ucfirst($kompetensi['nama_kompetensi']).'</span>', "<b>Kompetensi</b>"    ); 
	return  $view. table_builder($header , $datas ,  3 , false );
}

function form_kompetensi_indikator_create($pid, $id = 0){
	$view = form_header( "kompetensi" , "kompetensi"  );
 	$fields = my_get_data_by_id('kompetensi_indikator','kompetensi_indikator_id',$id);

	$tingkat = array(
				'name'=>'tingkat_id',
				'value'=>( isset($_POST['tingkat_id']) ? $_POST['tingkat_id'] : $fields['tingkat_id'] ),
				'id'=>'tingkat_id',
				'type'=>'text',
				'size'=>'20'
			);
	$form_field_tingkat = form_dynamic($tingkat);
	$view .= form_field_display( $form_field_tingkat   , "Tingkat");	

	$c = array(
				'name'=>'indikator',
				'value'=>( isset($_POST['indikator']) ? $_POST['indikator'] : $fields['indikator'] ),
				'id'=>'indikator',  
				'rows'=>'3',
				'size'=>'45'
			);
	$form_field_nama = form_textarea($c);
	$view .= form_field_display( $form_field_nama   , "Indikator");

	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
} 
	 
function form_kompetensi_indikator_submit($pid , $id =0){
	$datas = array(
		'kompetensi_id'=>my_type_data_int($pid),	
		'tingkat_id'=>my_type_data_int( $_POST['tingkat_id'] ),
		'indikator'=>my_type_data_str( $_POST['indikator'] ),
	);
	
	if($id > 0){
		return my_update_record('kompetensi_indikator' ,'kompetensi_indikator_id' , $id , $datas);
	}
	return my_insert_record('kompetensi_indikator' , $datas);
} 

function form_kompetensi_indikator_validate($kompetensi_indikator_id){
	$errsubmit = false;
	$err = array();
/*
	$tingkat_id =  trim($_POST["tingkat_id"]); 
	if( $nama == ''){
		$errsubmit = true;
		$err[] = "Kode kompetensi belum diisi";
	}

	$query="SELECT kompetensi_id FROM kompetensi 
		WHERE kode_kompetensi='".$kode.
		"' AND kompetensi_id<>".$kompetensi_id; 
	if (my_num_rows( my_query($query)) >0){
		$errsubmit = true;
		$err[] = "Kode sudah ada";	
	}
*/	
	$nama =  trim($_POST["indikator"]); 
	if( $nama == ''){
		$errsubmit = true;
		$err[] = "Indikator belum diisi";
	}
/*
	$query="SELECT kompetensi_id FROM kompetensi 
		WHERE nama_kompetensi='".$nama.
		"' AND kompetensi_id<>".$kompetensi_id; 
	if (my_num_rows( my_query($query)) >0){
		$errsubmit = true;
		$err[] = "Nama sudah ada";	
	}
 */   
	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}

function delete_kompetensi_indikator($kompetensi_indikator_id){
	$query = "DELETE FROM kompetensi_indikator WHERE kompetensi_indikator_id ={$kompetensi_indikator_id} ";
	return my_query($query);
}	

